package com.pwc.conversionTool.dataAdaptor;

import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class ExcelUtil extends BaseUtil {

	private InputStream in = null;

	private Workbook workBook;

	private final static String XSSFWorkbook = ".xlsx";

	private final static String HSSFWorkbook = ".xls";

	private String tabName;

	private Sheet sheet;

	

	public ExcelUtil(String fileName, InputStream in, String tabName)
			throws IOException {
		super();
		this.in = in;
		this.tabName = tabName;
		if (fileName.endsWith(XSSFWorkbook)) {
			this.workBook = new XSSFWorkbook(in);
		} else if (fileName.endsWith(HSSFWorkbook)) {
			this.workBook = new HSSFWorkbook(in);
		}

		this.sheet = workBook.getSheet(tabName);

		initialHeader();
		initialContent();
	}

	public ExcelUtil(InputStream in) throws IOException {
		super();
		this.in = in;
		this.workBook = new XSSFWorkbook(in);
	}

	public InputStream getIn() {
		return in;
	}

	public void setIn(InputStream in) {
		this.in = in;
	}

	public Workbook getWorkBook() {
		return workBook;
	}

	public void setWorkBook(XSSFWorkbook workBook) {
		this.workBook = workBook;
	}

	public String getTabName() {
		return tabName;
	}

	public void setTabName(String tabName) {
		this.tabName = tabName;
	}

	public void initialHeader() {
		sheet = workBook.getSheet(tabName);

		Row row = sheet.getRow(0);

		this.header = getRow(row);
		renameHeaderSameName();
	}

	@Override
	public String[] getHeader() {

		return header;
	}

	@Override
	public List<String[]> getContent() {
		
		return content;
	}

	public void initialContent() {
		sheet = workBook.getSheet(tabName);
		List<String[]> result = new ArrayList<String[]>();

		Iterator<Row> rowIterator = sheet.iterator();

		while (rowIterator.hasNext()) {
			Row row = rowIterator.next();
			result.add(getRow(row));
		}
		result.remove(0);

		this.content = result;
	}

	@Override
	public List<String[]> getAllData() {
		
		List<String[]> allData=new ArrayList<String[]>();
		
		allData.add(header);
		
		allData.addAll(content);	

		return allData;
	}

	@Override
	public int getColumnIndex(String ColumnName) {
		sheet = workBook.getSheet(tabName);
		Row row = sheet.getRow(0);
		String[] r = getRow(row);

		if (r != null && r.length != 0) {
			for (int i = 0; i < r.length; i++) {
				if (r[i].equals(ColumnName))
					return i;
			}
		}

		return -1;
	}

	private String[] getRow(Row row) {
		List<String> rowList = new ArrayList<String>();

		Iterator<Cell> cellIterator = row.cellIterator();

		while (cellIterator.hasNext()) {
			Cell cell = cellIterator.next();

			cell.setCellType(Cell.CELL_TYPE_STRING);
			rowList.add(cell.getStringCellValue());
		}

		String[] result = rowList.toArray(new String[rowList.size()]);

		return result;
	}

	@Override
	public void setContent(List<String[]> content) {
		this.content=content;
	}

}
